package com.aaa.system.service.impl;

import com.aaa.entity.User;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.UserService;
import com.aaa.system.vo.UserVo;
import com.aaa.utils.JwtUtil;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @program: his-open-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-03-20 10:55
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",username);
        wrapper.eq("status",0);
        return userDao.selectOne(wrapper);
    }

    @Override
    public Result<User> getUserInfo() {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        //根据手机号查询
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",map.get("username"));
        wrapper.eq("status",0);
        User user = userDao.selectOne(wrapper);
        return new Result<User>(2000,"查询用户成功",user);
    }

    @Override
    public Result<IPage<User>> findByCondition(Integer currentPage, Integer pageSize, UserVo userVo) {
        IPage<User> page=new Page<>(currentPage,pageSize);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(userVo.getUserName())){
             wrapper.like("user_name",userVo.getUserName());
        }
        if(StringUtils.hasText(userVo.getPhone())){
            wrapper.eq("phone",userVo.getPhone());
        }

        if(Objects.nonNull(userVo.getDeptId())){
            wrapper.eq("dept_id",userVo.getDeptId());
        }
        if(Objects.nonNull(userVo.getStatus())){
            wrapper.eq("status",userVo.getStatus());
        }
        if(Objects.nonNull(userVo.getDateRange())&&userVo.getDateRange().length==2){
             wrapper.between("create_time",userVo.getDateRange()[0],userVo.getDateRange()[1]);
        }


        IPage<User> page1 = userDao.selectPage(page, wrapper);
        return new Result<IPage<User>>(2000,"查询用户成功",page1);
    }

    @Override
    public List<User> queryUsersNeedScheduling(Map<String, Object> map) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(Objects.nonNull(map.get("userId"))){
            wrapper.eq("user_id",map.get("userId"));
        }
        if(Objects.nonNull(map.get("deptId"))){
            wrapper.eq("dept_id",map.get("deptId"));
        }
        //
        wrapper.eq("scheduling_flag",0);
        wrapper.eq("status",0);
        List<User> users = userDao.selectList(wrapper);
        return users;

    }
}






